//
//  ViewController.swift
//  QHSocketIODemo
//
//  Created by chen on 2017/5/12.
//  Copyright © 2017年 chen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    let socketIOClient = SocketIOClient(socketURL: URL(string: "ws://192.168.2.17:3000"/*"ws://10.7.242.242:3000"*//*"http://192.168.2.17:3080"*/)!, config: [.log(true)])//, .forcePolling(true)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        doSocketIO()
        
//        let socket = SocketIOClient(socketURL: URL(string: "ws://10.7.242.242:3080")!, config: [.log(true), .forcePolling(true)])
//        
//        socket.on(clientEvent: .connect) {data, ack in
//            print("socket connected")
//        }
//        
//        socket.on("currentAmount") {data, ack in
//            if let cur = data[0] as? Double {
//                socket.emitWithAck("canUpdate", cur).timingOut(after: 0) {data in
//                    socket.emit("update", ["amount": cur + 2.50])
//                }
//                
//                ack.with("Got your currentAmount", "dude")
//            }
//        }
//        
////        socketIOClient.on("message") { (data, ack) in
////            print("message==\(data)")
////        }
//        
//        socket.connect()
//        
////        DispatchQueue.global().asyncAfter(deadline: .now() + 3) {
////            self.socketIOClient.emit("message", with: ["hello"])
////        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func doSocketIO() {
        
        socketIOClient.connect(timeoutAfter: 3) {
            
//            self.socketIOClient.emit("login", [["userid":"123456789", "username":"哈哈"]])
            
            self.socketIOClient.on("message") { (data, ack) in
                print("message==\(data)")
            }
            
        }
//        //监听新用户登录
//        socketIOClient.on("login") { (data, ack) in
//            print("login==\(data)")
//        }
        
        //告诉服务器端有用户登录
//        socketIOClient.emit("login", with: [["userid" : "userid", "username" : "username"]]);
        
    }
    
    @IBAction func emitAction(_ sender: Any) {
        
        socketIOClient.emit("login", [["userid":"123456789", "username":"哈哈"]])
//        socketIOClient.emit("message", with: [["userid":"123456789", "username":"123", "content": "hello"]])
    }
    
}

class User {
    var userid = "123"
    var username = "123"
}

class Content {
    var userid = "123"
    var username = "123"
    var content = "hello"
}

